package com.fileshare.web.controller.index.api;

import com.fileshare.web.dto.Bo.UploadFileBo;
import com.fileshare.web.dto.Result;
import com.fileshare.web.dto.Vo.UploadFileVO;
import com.fileshare.web.service.UploadService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/upload")
@ResponseBody
public class UploadApiController {
    @Resource
    private UploadService uploadService;

    @GetMapping
    @PreAuthorize("@auth.matchAny('fileUploader:upload')")
    public Result<UploadFileVO> preUpload(UploadFileBo uploadFileBo) {
        UploadFileVO fileVO = null;
        if (uploadFileBo.getMd5Value() != null) {
            fileVO = UploadFileVO
                    .builder()
                    .skipped(true)
                    .uploaded(true)
                    .build();
        }
        return Result.success().with(fileVO);
    }

    @PostMapping
    @PreAuthorize("@auth.matchAny('fileUploader:upload')")
    public Result<UploadFileVO> upload(HttpServletRequest request, UploadFileBo fileBo) {
        UploadFileVO fileVO = uploadService.uploadFileFragment(request, fileBo);
        return Result.success().with(fileVO);
    }
}
